package com.bulldog.spider;

import com.bulldog.spider.console.ConsoleDepartListPipeLine;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.ConsolePageModelPipeline;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.MultiPagePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.processor.example.GithubRepoPageProcessor;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.selector.PlainText;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取北京人员信息
 */
public class BeijingProcessor implements PageProcessor {

    /**
     * text(n)	第n个直接文本子节点，为0表示所有	text() only
     * allText()	所有的直接和间接文本子节点	not support
     * tidyText()	所有的直接和间接文本子节点，并将一些标签替换为换行，使纯文本显示更整洁	not support
     * html()	内部html，不包括标签的html本身	not support
     * outerHtml()	内部html，包括标签的html本身	not support
     * regex(@attr,expr,group)	这里@attr和group均可选，默认是group0	not support
     */
    @Override
    public void process(Page page) {
//        page.putField("科室", page.getHtml().xpath("//div[@class='jt_bg  active']/a/tidyText()").toString());
//        page.putField("姓名", page.getHtml().xpath("//div[@class='zjtd-top-zj-list am-padding-0 am-margin-top-lg']/div/html()"));
//
        // 详情链接
         List<String> hrefList = page.getHtml().xpath("//a[@class='am-btn am-btn-danger am-btn-sm am-radius']/@href").all();
         page.addTargetRequests(hrefList);
         if (hrefList.size() > 0) {
             page.putField("姓名", page.getHtml().xpath("//h1[@class='am-margin-bottom-xs']/text()").toString().replaceAll(" /", ""));

             // 简介
             System.out.println(page.getHtml().xpath("//div[@class='am-fr']/div/p[1]/tidyText()"));
             System.out.println(page.getHtml().xpath("//div[@class='am-fr']/div/p[2]/tidyText()").toString().replaceAll("\n", ""));
             System.out.println();
             System.out.println();
             // 简历
             String info = page.getHtml().xpath("//div[@id='show-tab-1']/tidyText()").toString()  + "";
             System.out.println(info.replaceFirst("\n", ""));
         }

//        // 简介
//        List<String> briefList = page.getHtml().xpath("//div[@class='am-fr']/div/p[1]/text()").all();
//        // 擅长
//        List<String> skillList = page.getHtml().xpath("//div[@class='am-fr']/div/p[2]/text()").all();
//        page.putField("详情", hrefList);
//        page.putField("简介", briefList);
//        page.putField("擅长", skillList);
//        // 组合简介擅长
//        List<String> briefSkillList = new ArrayList<>();
//        if(briefList.size() == skillList.size() ){
//            for (int i = 0; i <briefList.size(); i++) {
//                briefSkillList.add("简介：" + briefList.get(i) + "\n" + "擅长："+skillList.get(i)+ "\n");
//            }
//        }
//        page.putField("个人信息链接：", SiteConst.DP_HOST + page.getHtml().xpath("//div[@class='am-fr']/div/div[@class='am-text-center]/a/@href").toString());

        System.out.println();
        System.out.println();
        System.out.println();
    }

    @Override
    public Site getSite() {
        return Site
                .me()
                .setDomain("daopei.net")
                .setSleepTime(1000)
                .setUserAgent(
                        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
    }

    public static void main(String[] args) {
        /**
         * 北京
         * https://daopei.net/list/29
         * https://daopei.net/list/30
         * https://daopei.net/list/31
         * https://daopei.net/list/32
         */
        String[] urls = {"https://daopei.net/list/29"};
//        String[] urls = {"https://daopei.net/list/29","https://daopei.net/list/30","https://daopei.net/list/31","https://daopei.net/list/32"};
        PageProcessor processor = new BeijingProcessor();
        OOSpider.create(processor)
                .addUrl(urls).thread(1)
                .addPipeline(new MultiPagePipeline())
                .addPipeline(new ConsolePipeline())
                .run();
    }

}